package com.wythe.mall.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.hb.dialog.myDialog.MyAlertDialog;
import com.wythe.mall.R;
import com.wythe.mall.activity.DeliveryStoreActivity;
import com.wythe.mall.activity.EvaluateActivity;
import com.wythe.mall.activity.OrderInfoActivity;
import com.wythe.mall.activity.PayOrderActivity;
import com.wythe.mall.adapter.ImageShopAdapter;
import com.wythe.mall.adapter.OrderListAdapter;
import com.wythe.mall.adapter.StoreOrderListAdapter;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.OrderBean;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.GotoActivity;
import com.wythe.mall.utils.SharePreferenceUtils;
import com.wythe.mall.utils.ToastUtil;
import com.wythe.mall.view.NoScrollGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class OrderFragment extends Fragment implements StoreOrderListAdapter.OnItemClickListener {
    private View view;
    private RecyclerView mRlOrderLists;
    private StoreOrderListAdapter mOrderAdapter;
    private List<OrderBean> mOrderLists;
    private int mId;
    private int status = 0;
    private boolean isPrepared = false;
    private LinearLayout mNodataView;

    public static OrderFragment getInstance(int status) {
        OrderFragment fragment = new OrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("status", status);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status = getArguments().getInt("status");
        if (mOrderLists == null) {
            mOrderLists = new ArrayList<>();
        }
        mOrderLists.clear();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_order_item, container, false);
        isPrepared = true;
        initView();
        getOrderList();
        return view;
    }

    private void initView() {
        mRlOrderLists = view.findViewById(R.id.rl_order);
        mNodataView = view.findViewById(R.id.fragment_noDataLayout);
        mOrderAdapter = new StoreOrderListAdapter(getActivity(), mOrderLists);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRlOrderLists.setLayoutManager(linearLayoutManager);//设置布局管理器
        mRlOrderLists.setAdapter(mOrderAdapter);
        mOrderAdapter.setOnItemClickListener(this);
    }

    public void getOrderList() {
        JSONObject param = new JSONObject();
        try {
            param.put("limit", 50);
            param.put("page", 0);
            param.put("status", status);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiOkHttp.getOrderListLists(getActivity(), param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                mOrderLists.clear();
                mNodataView.setVisibility(View.GONE);
                JSONObject jsonObject = null;
                try {
                    jsonObject = data.getJSONObject("data");
                    JSONArray records = jsonObject.getJSONArray("records");
                    for (int i = 0; i < records.length(); i++) {
                        OrderBean bean = new Gson().fromJson(records.getJSONObject(i).toString(), OrderBean.class);
                        mOrderLists.add(bean);
                    }
                    mOrderAdapter.setDatas(mOrderLists);
                    if (mOrderLists.size() == 0) {
                        mNodataView.setVisibility(View.VISIBLE);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isPrepared && isVisibleToUser) {
            getOrderList();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(int position, OrderBean bean) {
        Bundle bundle = new Bundle();
        bundle.putString("orderNo", bean.getOrderNo());
        GotoActivity.gotoActiviy(getActivity(), OrderInfoActivity.class, bundle);
    }

    @Override
    public void onPayClick(int position, OrderBean bean) {
        Bundle bundle = new Bundle();
        bundle.putString("orderNo", bean.getOrderNo());
        bundle.putDouble("payAmount", bean.getTotalAmount());
        GotoActivity.gotoActiviy(getActivity(), PayOrderActivity.class, bundle);
    }

    @Override
    public void onCancelClick(int position, OrderBean bean) {
        JSONObject param = new JSONObject();
        try {
            param.put("orderNo", bean.getOrderNo());
            param.put("status", 2);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.editOrderList(getActivity(), param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                try {
                    int code1 = data.getInt("code");
                    if (code == 200) {
                        getOrderList();
                    }
                    ToastUtil.show(getActivity(), data.getString("msg"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }

    @Override
    public void onConfirmClick(int position, final OrderBean bean) {
        MyAlertDialog myAlertDialog = new MyAlertDialog(getActivity());
        myAlertDialog.builder();
        myAlertDialog.setTitle("温馨提示");
        myAlertDialog.setMsg("你是否确认收货？");
        myAlertDialog.setPositiveButton("确认", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject param = new JSONObject();
                try {
                    param.put("orderNo", bean.getOrderNo());
                    param.put("status", 7);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                ApiOkHttp.editOrderList(getActivity(), param, new OnRequestDataListener() {
                    @Override
                    public void requestSuccess(int code, JSONObject data) {
                        try {
                            int code1 = data.getInt("code");
                            if (code == 200) {
                                getOrderList();
                            }
                            ToastUtil.show(getActivity(), data.getString("msg"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void requestFailure(int code, String msg) {

                    }
                });
            }
        });
        myAlertDialog.setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        myAlertDialog.show();

    }

    @Override
    public void onRecommedClick(int position, OrderBean bean) {
        Bundle bundle = new Bundle();
        bundle.putString("orderNo",bean.getOrderNo());
        bundle.putSerializable("list", (Serializable) bean.getItemVo());
        GotoActivity.gotoActiviy(getActivity(), EvaluateActivity.class, bundle);
    }
}
